// Lottery2Doc.cpp : implementation of the CLottery2Doc class
//

#include "stdafx.h"
#include "Lottery2.h"

#include "Lottery2Doc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CLottery2Doc

IMPLEMENT_DYNCREATE(CLottery2Doc, CDocument)

BEGIN_MESSAGE_MAP(CLottery2Doc, CDocument)
END_MESSAGE_MAP()


// CLottery2Doc construction/destruction

CLottery2Doc::CLottery2Doc()
{
	// TODO: add one-time construction code here

	m_id_match=0;
	m_bNewPage=FALSE;

}

CLottery2Doc::~CLottery2Doc()
{
	if (m_cntMatch.connected())
		m_cntMatch.disconnect();
}

BOOL CLottery2Doc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}




// CLottery2Doc serialization

void CLottery2Doc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}


// CLottery2Doc diagnostics

#ifdef _DEBUG
void CLottery2Doc::AssertValid() const
{
	CDocument::AssertValid();
}

void CLottery2Doc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG


// CLottery2Doc commands

void CLottery2Doc::onMatch(int id,BOOL bNewPage,BOOL& bResult)
{
	if (bResult)
		return;
	if (id==m_id_match)
	{
		bResult=TRUE;
		m_bNewPage|=bNewPage;
	}
	else if (!m_bNewPage)
	{
		SetMatch(id,bNewPage);
		bResult=TRUE;
	}
	if (bResult)
	{
		//active the window
		POSITION pos=GetFirstViewPosition();
		if (pos!=NULL)
		{
			CView* pView=GetNextView(pos);
			ASSERT_VALID(pView);
			CFrameWnd* pFrame = pView->GetParentFrame();

			if (pFrame == NULL)
				TRACE(traceAppMsg, 0, "Error: Can not find a frame for document to activate.\n");
			else
			{
				pFrame->ActivateFrame();
				if (pFrame->GetParent() != NULL)
				{
					CFrameWnd* pAppFrame;
					if (pFrame != (pAppFrame = (CFrameWnd*)AfxGetApp()->m_pMainWnd))
					{
						ASSERT_KINDOF(CFrameWnd, pAppFrame);
						pAppFrame->ActivateFrame();
					}
				}
			}
		}
	}
}

void CLottery2Doc::SetMatch(int id,BOOL bNewPage)
{
	m_id_match=id;
	m_bNewPage=bNewPage;
	CString csTitle;
	csTitle.Format("Match:%d",id);
	SetTitle(csTitle);
}